#!/bin/bash

#OLPC
#gxlxnis@lxptop.org | sed s/x/a/
#olpc-connections v0.3

init () {
#btime=`awk '/btime/ {print $2}' /proc/stat`
#echo " "`date -d "Jan 1 1970 00:00 UTC + $btime seconds" +%H:%M:%S`" : boot"
#echo " "`date +%H:%M:%S`" : script started"
echo
space="                    "
echo "          |      msh0       |      eth0       |       dns       |salut|gabbl|     jabber      | xos"
}

stats () {
ipmsh=`ifconfig msh0|grep "inet addr"|sed s/^.*r://|awk '{print $1}'`
ipeth=`ifconfig eth0|grep "inet addr"|sed s/^.*r://|awk '{print $1}'`
dns=`awk '/^name/ {print $2}' /etc/resolv.conf|awk 'NR==1'`
salut=`sugar-telepathies|awk '/salut/ {print $2}'`
gabble=`sugar-telepathies|awk '/gabble/ {print $2}'`
jabber=`netstat -ntpT 2> /dev/null|grep telepathy|grep ESTAB|awk '{print $5}'|sed s/:.*$//`
xos=`sugar-xos|wc -l`
[ "$salut" == "0" ] && salut=" on  "
[ "$salut" == "1" ] && salut=" ... "
[ "$salut" == "2" ] && salut=" off "
[ "$salut" == "" ] && salut="     "
[ "$gabble" == "0" ] && gabble=" on  "
[ "$gabble" == "1" ] && gabble=" ... "
[ "$gabble" == "2" ] && gabble=" off "
[ "$gabble" == "" ] && gabble="     "

let sp=15-${#ipmsh};let sp1=$sp/2;let sp2=$sp-$sp1;echo -n "${space:0:$sp1}$ipmsh${space:0:$sp2} | "
let sp=15-${#ipeth};let sp1=$sp/2;let sp2=$sp-$sp1;echo -n "${space:0:$sp1}$ipeth${space:0:$sp2} | "
let sp=15-${#dns};let sp1=$sp/2;let sp2=$sp-$sp1;echo -n "${space:0:$sp1}$dns${space:0:$sp2} |"
echo -n "$salut|"
echo -n "$gabble| "
let sp=15-${#jabber};let sp1=$sp/2;let sp2=$sp-$sp1;echo -n "${space:0:$sp1}$jabber${space:0:$sp2} | "
echo -e "$xos"
}

log () {
stats1=`stats`
echo -n " "`date +%H:%M:%S`
echo " | $stats1"

while true
do
	sleep 1
	stats2=`stats`
	[ ! "$stats2" = "$stats1" ] && echo -n " "`date +%H:%M:%S` && echo " | $stats2"
	stats1="$stats2"
done 
}

init
log
